﻿Public Class FormMain
    Dim objUser As New clsUser
    Dim objCA As New clsCPCK
    Private dtCacheArea As DataTable
    Private _search_status As String
    Dim txt As TextBox
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dgv.Font = New Font("UTF-8", 14)
        connectDB()
        getUserListByCJ()
        Dim startDate As String = Date.Now.ToShortDateString()
        txt_into_time_start.Text = startDate
        txt_out_time_start.Text = startDate
        Dim endDate As String = Date.Now.ToShortDateString()
        txt_into_time_end.Text = endDate
        txt_out_time_end.Text = endDate
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_Warehouse_YZZH.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If
    End Sub

    Private Sub getUserListByCJ()
        Dim dtUser As DataTable = objUser.getUserListByCJ(2)
        cmbOper.DisplayMember = "user_name"
        cmbOper.ValueMember = "user_id"
        cmbOper.DataSource = dtUser
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        If rdoBusy.Checked Then
            _search_status = "0"
        Else
            _search_status = "1"
        End If
        dtCacheArea = objCA.getList(_search_status, txtProdId.Text.Trim(), cmbWHNo.Text,
                                    txt_into_time_start.Text.Trim(),
                                    txt_into_time_end.Text.Trim(),
                                    txt_out_time_start.Text.Trim(),
                                    txt_out_time_end.Text.Trim())
        dtCacheArea.Columns.Add("status_name")
        If dtCacheArea.Rows.Count > 0 Then
            For Each row As DataRow In dtCacheArea.Rows
                If row("status") = "0" Then
                    row("status_name") = "在库"
                Else
                    row("status_name") = "已出库"
                End If
            Next
        End If
        Dim dv As DataView = dtCacheArea.DefaultView
        dv.Sort = "hour_length desc"
        Dim dtCopy As DataTable = dv.ToTable()
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dtCopy
    End Sub

    Private Sub btnRk_Click(sender As Object, e As EventArgs) Handles btnRk.Click
        Dim formRk As New FormRk()
        formRk.usr = cmbOper.Text.Trim()
        formRk.ShowDialog(Me)
    End Sub

    Private Sub btnDELIVER_Click(sender As Object, e As EventArgs) Handles btnDELIVER.Click
        Dim formDeliver As New FormDeliver()
        formDeliver.usr = cmbOper.Text.Trim()
        formDeliver.ShowDialog(Me)
    End Sub

    Private Sub txt_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_into_time_start.MouseClick, txt_into_time_end.MouseClick, txt_out_time_start.MouseClick, txt_out_time_end.MouseClick
        txt = CType(sender, TextBox)
        DateNavigator1.Left = txt.Left
        DateNavigator1.Top = txt.Location.Y + txt.Height
        DateNavigator1.Visible = True
    End Sub


    Private Sub DateNavigator1_DateTimeChanged(sender As Object, e As EventArgs) Handles DateNavigator1.DateTimeChanged
        txt.Text = DateNavigator1.DateTime.ToShortDateString()
        DateNavigator1.Visible = False
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub
End Class
